# Makefile for use with LaTeX documents
# copyright 2000-2012 Charl P. Botha <info@charlbotha.com>

##### NOTES #####

# to make hyperlinks a table-of-contents and other groovy stuff happen in
# your pdf comment, add the following two lines to your .tex document
#\usepackage[ps2pdf,bookmarks,linktocpage,colorlinks=true]{hyperref}
#\usepackage[ps2pdf]{thumbpdf}

# to add other metadata, use hypersetup:
# \hypersetup{%
# pdftitle={Techniques and Software Architectures for Biomedical
# Visualisation and Image Processing},
# pdfauthor={Charl P. Botha},
# pdfkeywords={visualisation, visualization, image processing,
# biomedical}
# }%

# this Makefile:
# make: makes the dvi
# make ps: makes the ps
# make pdf: makes the PDF, doh.
# make clean: nukes everything

# embed highest quality figures: (i.e. don't use lossy DCT)
# make HQFIGURES=yes pdf

# pass through PDFSETTINGS to ps2pdf
# make PDFSETTINGS=pvalue pdf
# where pvalue can be default, screen, ebook, printer, prepress
# you usually need prepress for final submission!

###### You have to setup the following variables ######

# i.e. your documents are/will be called basename.tex, basename.ps.
# basename.pdf etc.
TARGET_BASENAME = pt

# use this variable if you have a separate tex file for each chapter
# these will get added to the dependencies
# if you have no other tex files besides TARGET_BASENAME.tex, just
# leave this empty.
OTHER_LATEX_DEPS = devide.tex \
segmentation.tex shellsplatting.tex tfSpecification.tex

# change this to yes if your document has a bibiliography, no otherwise
BIB = no
# this can also be e.g. ../../references/references.bib or somesuch
# also remember to adapt your \bibliography{} statement accordingly, e.g.
# \bibliography{../references/references} (yes, without the .bib)
BIBFILE = references.bib

# these are the figures on which your document depends.  these should be
# specified as EPS format figures. you usually don't
# have to do the conversions yourself: if the Makefile sees that you have
# e.g. figure1.pdf lying around, but your document depends on figure1.eps,
# it will automatically convert figure1.pdf to EPS
# if you have no figures, comment this out
DVIFIGS1 = image1.eps image2.eps

# FIXPREFX should indicate the subdirectory that contains all your
# figures in this case, all your figures should be in the subdir
# figures/ if you don't like this, just comment FIGPREFIX out, but
# leave DVIFIGS = ...
FIGPREFIX = figures
DVIFIGS = $(addprefix $(FIGPREFIX)/, $(DVIFIGS1))

####### Nothing to do below this line... move along... #####

# this is just to make sure that if I make something gets done (you don't
# have to modify this)
all: dvi

# -Ppdf -G0 makes sure that type1 postscript fonts are used
# -z makes sure that hyperlinks (for instance via hyperref) go through
DVIPS = dvips -z -t a4 -Ppdf -G0
# these are the parameters that you
# should use according to ACM siggraph
# see: http://partners.adobe.com/asn/acrobat/docs/distparm.pdf
PS2PDF = ps2pdf \
-dCompatibilityLevel\#1.3 -dMaxSubsetPct\#100 \
-dSubsetFonts\#true -dEmbedAllFonts\#true
#SHELL = sh

ifeq ($(HQFIGURES),yes)
  PS2PDF += -dAutoFilterColorImages\#false -dAutoFilterGrayImages\#false \
-dColorImageFilter\#/FlateEncode -dGrayImageFilter\#/FlateEncode \
-dMonoImageFilter\#/FlateEncode \
-dDownsampleColorImages\#false -dDownsampleGrayImages\#false
endif

# see: http://www.cs.wisc.edu/~ghost/doc/AFPL/8.00/Ps2pdf.htm
# options are: default, screen, ebook, print, prepress
ifdef PDFSETTINGS
  PS2PDF += -dPDFSETTINGS\#/$(PDFSETTINGS)
endif

# src-specials are so that inverse searching works in yap
LATEX = latex --src-specials

# these are the pure latex sources
LTXSOURCES = $(TARGET_BASENAME).tex $(OTHER_LATEX_DEPS)
ifeq ($(BIB),yes)
  LTXSOURCES += $(TARGET_BASENAME).bbl
endif

# these are the sources if we're making DVIs (figures first, NB)
DVISOURCES = $(DVIFIGS) $(LTXSOURCES) 

dvi: $(TARGET_BASENAME).dvi

$(TARGET_BASENAME).dvi: $(DVISOURCES)
	$(LATEX) $(TARGET_BASENAME)
	if [ ! -z "`grep "Citation .* undefined" $(TARGET_BASENAME).log`" ]; then \
	 bibtex $(TARGET_BASENAME); \
	 $(LATEX) $(TARGET_BASENAME); \
	 $(LATEX) $(TARGET_BASENAME); \
	fi
	if [ ! -z "`grep "Rerun to get cross-references right." $(TARGET_BASENAME).log`" ]; then \
	 $(LATEX) $(TARGET_BASENAME); \
	fi
	$(LATEX) $(TARGET_BASENAME)

ps : $(TARGET_BASENAME).ps

# USE THIS CLAUSE INSTEAD OF THE ONE ABOVE IF YOU'RE WORKING WITH BIBUNITS
# (remember to remove _bibunits from this target name, and to add something
#  else to the one above, hmmmkay?)
# if latex uses bibunits, run of latex will generate number of bu?.aux
# files; we need to run bibtex on these too to generate BBL files
$(TARGET_BASENAME)_bibunits.dvi: $(DVISOURCES)
	$(LATEX) $(TARGET_BASENAME)
	if [ ! -z "`grep "Citation .* undefined" $(TARGET_BASENAME).log`" ]; then \
	 for bu_file in bu?.aux; do bibtex `echo $$bu_file | cut -f 1 -d .`; done; \
	 $(LATEX) $(TARGET_BASENAME); \
	 $(LATEX) $(TARGET_BASENAME); \
	fi
	if [ ! -z "`grep "Rerun to get cross-references right." $(TARGET_BASENAME).log`" ]; then \
	 $(LATEX) $(TARGET_BASENAME); \
	fi
	$(LATEX) $(TARGET_BASENAME)


$(TARGET_BASENAME).ps: $(TARGET_BASENAME).dvi
	$(DVIPS) -o $@ $<

pdf : $(TARGET_BASENAME).pdf

$(TARGET_BASENAME).pdf: $(TARGET_BASENAME).ps
	$(PS2PDF) $< $@

$(TARGET_BASENAME).bbl : $(BIBFILE)
	if [ ! -f $(TARGET_BASENAME).aux ]; then $(LATEX) $(TARGET_BASENAME); fi
	bibtex $(TARGET_BASENAME)

%.eps: %.dia
	dia --nosplash --export=$@ $<

%.eps: %.obj
	tgif -eps -print $<

%.pdf: %.eps
	epstopdf $<

%.ps: %.mp
	mpost $<

%.ps: %.dot
	dot -Tps $< -o $@


# epstopdf does a fine job of this conversion IF your boundingbox in the ps
# is correct.
%.pdf: %.ps
	epstopdf $<

# for this, you need jpeg2ps from: 
# http://www.pdflib.com/products/more/jpeg2ps.html
# it's absolutely worth the trouble.
%.eps: %.png
	convert -quality 100 $< temp.jpg
	jpeg2ps temp.jpg > $@

%.eps: %.pdf
	pdftops -eps $< $@

%.eps: %.jpg
	jpeg2ps $< > $@

# this is the old rule for bitmaps
#%.eps: %.png
#	convert $< temp.eps
#	eps2eps temp.eps $@
#	rm temp.eps

# clean this directory out
clean :
	rm -f *.aux $(TARGET_BASENAME).dvi $(TARGET_BASENAME).ps $(TARGET_BASENAME).pdf *.log *.lof *.lot *.blg *.bbl *.toc *~ *.mpx *.out
